package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String index(Model model){

        List<Region> regions = regionService.queryHotRegions();
        model.addAttribute("hotRegions",regions);
        return "destination/index";
    }

    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model,Long regionId){

        //查询所有目的地
        List<Destination> list = destinationService.getDestByRegionId(regionId);
        int mid = list.size()/2;
        List<Destination> leftlist = list.subList(0, mid);
        List<Destination> rightList = list.subList(mid, list.size());

        model.addAttribute("leftDests",leftlist);
        model.addAttribute("rightDests",rightList);
        model.addAttribute("regionId",regionId);

        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public String guide(Model model,Long id){



        List<Destination> toasts = destinationService.getToasts(id);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);

        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.selectCatalogByDestId(id);
        model.addAttribute("catalogs",catalogs);

        //strategyDetails
        List<StrategyDetail> list = strategyDetailService.getViewnumTop3(id);
        model.addAttribute("strategyDetails",list);


        return "destination/guide";

    }

    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);

        return "destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(Model model,@ModelAttribute("qo")StrategyCatalogQuery qo){
        //获取catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.selectCatalogByDestId(qo.getDestId());
        StrategyCatalog catalog=null;
        if(catalogs !=null && catalogs.size()>0){
            //后去catalog
            for (StrategyCatalog c : catalogs) {
                if(c.getId() == qo.getCatalogId()){
                    catalog = c;
                    model.addAttribute("catalog",c);
                }
            }
        }
        model.addAttribute("catalogs",catalogs);
        //获取detail
        if(catalog != null){
            List<StrategyDetail> details = catalog.getDetails();
            if(details != null && details.size() > 0){
                StrategyDetail detail = details.get(0);
                StrategyContent content = strategyDetailService.getContent(detail.getId());
                detail.setStrategyContent(content);
                model.addAttribute("detail",detail);
            }
        }
        return "destination/surveyTpl";
    }

    @RequestMapping("/travels")
    public String travels(Model model,@ModelAttribute("qo") TravelQuery qo){

        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "destination/travelTpl";
    }


}
